package com.order.center.controller;

import com.order.center.iclent.IPayCenterClient;
import com.pay.center.common.req.PaymentReq;
import com.pay.center.common.resp.RespPayData;
import com.pay.center.common.resp.Response;
import com.pay.center.common.utils.PayDigestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private IPayCenterClient payCenterClient;

    /**
     *
     */
//    @Transactional
    @RequestMapping("/pay")
    public String orderPay(){

        //调用支付中心接口,
//        "payChannel":"ALIPAY",
//                "payMethod":"",
//                "orderAmount":"",
//                "orderId":"20210310110677",
//                "goodsDesc":"ssd",
//                "mchId":"",
//                "returnUrl":"",
//                "signType":"md5",
//                "sign":"sdsadasdsa",
//                "userId":"10086",
//                "timestamp":"20210311"

        PaymentReq paymentReq = new PaymentReq();
        paymentReq.setUserId("test1234");
        paymentReq.setGoodsDesc("ssd");
        paymentReq.setMchId("1000");
        paymentReq.setPayChannel("ALIPAY");
        paymentReq.setOrderId("20210310110679");
        paymentReq.setPayMethod("alipay.trade.page.pay");
        paymentReq.setReturnUrl("www.baidu.com");
        paymentReq.setOrderAmount("100");
        paymentReq.setTimestamp(System.currentTimeMillis()+"");

        try {
            String sign = PayDigestUtil.getSign(paymentReq,"test123");

            paymentReq.setSignType("MD5");
            paymentReq.setSign(sign);

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }


        Response<RespPayData> response = payCenterClient.pay(paymentReq);

        System.out.println("===response===" + response);


        return response.getCode()+"";

    }


    /**
     * 创建订单
     */
    public void create(){

            //1.往订单表插数据 insert order表

            //2.写消息放到，延时队列。30分内必须付款，不然关闭订单 放入 order-queue

    }
}
